package com.koron.order.experiment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.experiment.bean.po.ExperimentItemParamsPo;
import com.koron.order.experiment.mapper.ExperimentItemParamsMapper;
import com.koron.order.experiment.service.ExperimentItemParamsService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * (ExperimentItemParams)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-09 09:09:45
 */
@Service("experimentItemParamsService")
public class ExperimentItemParamsServiceImpl implements ExperimentItemParamsService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, ExperimentItemParamsPo experimentItemParams) {

        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);

        if (StringUtils.isEmpty(experimentItemParams.getId())) {
            experimentItemParams.setId(CodeTools.getCode32());
        }

        return mapper.insertOrUpdate(experimentItemParams);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<ExperimentItemParamsPo> experimentItemParamsList) {

        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);

        if (CollectionUtils.isEmpty(experimentItemParamsList)) {
            return 0;
        }
        experimentItemParamsList.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });

        return mapper.insertOrUpdateBatch(experimentItemParamsList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, ExperimentItemParamsPo experimentItemParams) {

        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);

        if (StringUtils.isEmpty(experimentItemParams.getId())) {
            experimentItemParams.setId(CodeTools.getCode32());
        }

        return mapper.insert(experimentItemParams);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<ExperimentItemParamsPo> experimentItemParamsList) {

        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);

        if (CollectionUtils.isEmpty(experimentItemParamsList)) {
            return 0;
        }
        experimentItemParamsList.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });
        //先根据试验工单id删除
        //mapper.deleteByBusinessId(experimentItemParamsList.get(0).getExperimentId(), "", "");
        return mapper.insertBatch(experimentItemParamsList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, ExperimentItemParamsPo experimentItemParams) {

        Assert.hasText(experimentItemParams.getId(), "主键id不能为空");
        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);
        return mapper.update(experimentItemParams);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByItemId")
    public int deleteByItemId(SessionFactory factory, String itemId) {
        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);
        return mapper.deleteByItemId(itemId);
    }

    @Override
    @TaskAnnotation("deleteByExperimentId")
    public int deleteByExperimentId(SessionFactory factory, String experimentId) {
        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);
        return mapper.deleteByBusinessId(experimentId, "", "");
    }

    @Override
    @TaskAnnotation("queryById")
    public ExperimentItemParamsPo queryById(SessionFactory factory, String id) {

        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<ExperimentItemParamsPo> queryList(SessionFactory factory, ExperimentItemParamsPo experimentItemParams) {

        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);

        List<ExperimentItemParamsPo> list = mapper.queryList(experimentItemParams);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, ExperimentItemParamsPo experimentItemParams, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());
        ExperimentItemParamsMapper mapper = factory.getMapper(ExperimentItemParamsMapper.class);

        List<ExperimentItemParamsPo> list = mapper.queryList(experimentItemParams);

        return new PageInfo(list);
    }

}